package com.dongdongshop.controller;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SendController {

    @Autowired
    private Client client;

    @Value("${aliyun.signName}")
    private String signName;

    @Value("${aliyun.templateCode}")
    private String templateCode;

    @ResponseBody
    @RequestMapping("sendMessage")
    public String sendMessage(String phoneNum){
        //自动生成四位数验证码
        String numeric = RandomStringUtils.randomNumeric(4);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNum)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam("{\"sms_code\" : \""+ numeric +"\"}");
        //复制代码运行请自行打印API的返回值
        try {
            client.sendSms(sendSmsRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";
    }
}
